package com.rm.zeyou.pay.wxpay.service.impl;


import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.ApplicationConfigMapper;
import com.rm.zeyou.pay.wxpay.constants.WechatH5LoginUrlConstants;
import com.rm.zeyou.pay.wxpay.entity.GetAccessTokenByCodeBean;
import com.rm.zeyou.pay.wxpay.entity.GetUserInfoBean;
import com.rm.zeyou.pay.wxpay.service.WechatAppLoginService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.http.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Description: 微信公众号登陆
 * @Title: package-info
 * @Package com.sxskz.software.base.login.wx.miniprogram.impl
 * @Author ZhangKeChen  🤙🤙🏼🤙🏽
 * @Date 2023/4/26 5:45 PM
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class WechatAppLoginServiceImpl implements WechatAppLoginService {

    // @Resource
    // private RestTemplate restTemplate;

    //@Resource
    //private WxPayEntity wxPayEntity;
    @Resource
    private HttpClientUtil httpClientUtil;
    @Resource
    private ApplicationConfigMapper applicationConfigMapper;


    /**
     * 通过code换取网页授权access_token
     *
     * @return
     */
    @Override
    public GetAccessTokenByCodeBean getAccessToken(String code,Integer type) {
        String appAppid ;
        String appSecret ;
        if (type==1){
            //用户
            appAppid = applicationConfigMapper.selectOneData("app_appid");
            appSecret = applicationConfigMapper.selectOneData("app_secret");
        }else {
            //技师
            appAppid = applicationConfigMapper.selectOneData("technician_app_appid");
            appSecret = applicationConfigMapper.selectOneData("technician_app_secret");
        }
        HashMap<String, String> requestUrl = new HashMap<String, String>(4);
        requestUrl.put("appid", appAppid);
        requestUrl.put("secret", appSecret);
        requestUrl.put("code", code);
        requestUrl.put("grant_type", "authorization_code");
        GetAccessTokenByCodeBean getAccessTokenByCodeBean = httpClientUtil.doGet(WechatH5LoginUrlConstants.GetAccessTokenByCode, requestUrl, GetAccessTokenByCodeBean.class);
        log.info("通过code换取网页授权access_token:[{}]", getAccessTokenByCodeBean);
        if (CheckEmptyUtil.isEmpty(getAccessTokenByCodeBean.getAccessToken())) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ABNORMAL_OPERATION);

        }
        return getAccessTokenByCodeBean;
    }

    /**
     * 拉取用户信息
     */
    @Override
    public GetUserInfoBean getUserInfo(String openId, String accessToken) {
        HashMap<String, String> requestUrl = new HashMap<String, String>(4);
        requestUrl.put("access_token", accessToken);
        requestUrl.put("openid", openId);
        requestUrl.put("lang", "zh_CN");
        GetUserInfoBean getUserInfoBean = httpClientUtil.doGet(WechatH5LoginUrlConstants.GetUserInfo, requestUrl, GetUserInfoBean.class);
        log.info("拉取用户信息:[{}]", getUserInfoBean);
        if (CheckEmptyUtil.isEmpty(getUserInfoBean.getOpenid())) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ABNORMAL_OPERATION);
        }
        return getUserInfoBean;
    }


}
